﻿using log4net.Config; 
using smforj.common.model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Linq;
using System.Text;

namespace smforj.common.dao
{
    public interface IFittingDao
    {
        /// <summary>
        /// 添加商品推荐信息
        /// </summary>
        /// <param name="list">推荐信息对象集</param>
        /// <returns></returns>
        int SaveBatch(List<Recommend> list);
        /// <summary>
        /// 添加商品推荐信息
        /// </summary>
        /// <param name="model">推荐信息对象</param>
        /// <returns></returns>
        int AddRecommend(Recommend model);

        /// <summary>
        /// 查询本地商品推荐信息
        /// </summary>
        /// <returns></returns>
        List<Recommend> GetRecommendsLocal(IQuery query);  

        /// <summary>
        /// 根据id删除推荐信息
        /// </summary>
        /// <param name="id">推荐信息id</param>
        /// <returns></returns>
        int DeleteById(String id);

        /// <summary>
        /// 保存推荐信息,推荐信息为临时信息，每个位置只保存一份，每次保存前会删除之前的数据
        /// </summary>
        /// <param name="list">推荐信息列表</param>
        /// <returns>-1 失败 其他 成功</returns>
        int SaveCollocationBatch(List<Collocation> list);

        /// <summary>
        /// 根据位置信息查询要显示的信息
        /// </summary>
        /// <param name="location"></param>
        /// <returns></returns>
        List<Collocation> GetCollocationList(String location);

    }


    public class FittingSqliteDaoImpl : IFittingDao
    {
        //private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        //private static SqliteDB sqldb = null;

        //private string configFile = "";
        //public FittingSqliteDaoImpl()
        //{
        //    configFile = AppDomain.CurrentDomain.BaseDirectory + @"Log.config";
        //    XmlConfigurator.Configure(new System.IO.FileInfo(configFile));

        //    if (sqldb == null)
        //        sqldb = SQLiteHelper.GetInstance();
        //}
        ///// <summary>
        ///// 添加商品推荐信息
        ///// </summary>
        ///// <param name="model"></param>
        ///// 
        ///// <returns></returns>
        //public int AddRecommend(model.Recommend model)
        //{
        //    try
        //    {
        //        String sql = String.Format("INSERT INTO `recommend` (`id`, `rfid1`, `rfid2`, `rfid3`, `rfid4`, `rfid5`, `status`, `is_deleted`, `remark`, `create_date`, `update_date`)  VALUES ('{0}','{1}', '{2}', '{3}', '{4}','{5}', {6},{7},'{8}', NOW(), NOW());"
        //        , Guid.NewGuid().ToString()
        //        , model.rfid1 == null ? "" : model.rfid1
        //        , model.rfid2 == null ? "" : model.rfid2
        //        , model.rfid3 == null ? "" : model.rfid3
        //        , model.rfid4 == null ? "" : model.rfid4
        //        , model.rfid5 == null ? "" : model.rfid5
        //        , 0
        //        , 0
        //        , model.remark);
        //        return sqldb.ExecuteNonQueryTrans(CommandType.Text, sql);
        //    }
        //    catch (SQLiteException ex)
        //    {
        //        log.Error("AddRecommend " + ex.Message);
        //        return -1;
        //    }
        //}

        ///// <summary>
        ///// 根据id删除商品推荐信息
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //public int DeleteById(string id)
        //{
        //    int iRes = -1;
        //    try
        //    {
        //        String sql = String.Format("DELETE FROM `recommend` where id ='{0}'", id);
        //        iRes = sqldb.ExecuteNonQueryTrans(CommandType.Text, sql);
        //    }
        //    catch (SQLiteException ex)
        //    {
        //        log.Error("DeleteById " + ex.Message);
        //        iRes = -1;
        //    }
        //    return iRes;
        //}
        ///// <summary>
        ///// 批量保存商品推荐信息
        ///// </summary>
        ///// <param name="list"></param>
        ///// <returns></returns>
        //public int SaveBatch(List<Recommend> list)
        //{
        //    DbProviderFactory factory = SQLiteFactory.Instance;
        //    using (DbConnection conn = factory.CreateConnection())
        //    {
        //        DbTransaction trans = null;
        //        DbCommand cmd = null;
        //        try
        //        {
        //            // 连接数据库  
        //            conn.ConnectionString = sqldb.ConnectionString;
        //            conn.Open();
        //            trans = conn.BeginTransaction();
        //            String sql = "";
        //            cmd = conn.CreateCommand();
        //            cmd.Connection = conn;
        //            foreach (Recommend model in list)
        //            {
        //                sql = "INSERT INTO `recommend` (`id`, `rfid1`, `rfid2`, `rfid3`, `rfid4`, `rfid5`, `status`, `is_deleted`, `remark`, `create_date`, `update_date`) " +
        //                    " VALUES (@id, @rfid1, @rfid2, @rfid3, @rfid4, @rfid5, @status, @is_deleted, @remark, @create_date, @update_date);";

        //                SQLiteParameter[] parameters = {
        //                new SQLiteParameter("@id", DbType.String),
        //                new SQLiteParameter("@rfid1", DbType.String),
        //                new SQLiteParameter("@rfid2", DbType.String),
        //                new SQLiteParameter("@rfid3", DbType.String),
        //                new SQLiteParameter("@rfid4", DbType.String),
        //                new SQLiteParameter("@rfid5", DbType.String),
        //                new SQLiteParameter("@status", DbType.Int32,4),
        //                new SQLiteParameter("@is_deleted", DbType.Int32,4),
        //                new SQLiteParameter("@remark", DbType.String),
        //                new SQLiteParameter("@create_date", DbType.DateTime), 
        //                new SQLiteParameter("@update_date", DbType.DateTime)};
        //                parameters[0].Value = Guid.NewGuid().ToString();
        //                parameters[1].Value = model.rfid1 == null ? "" : model.rfid1;
        //                parameters[2].Value = model.rfid2 == null ? "" : model.rfid2;
        //                parameters[3].Value = model.rfid3 == null ? "" : model.rfid3;
        //                parameters[4].Value = model.rfid4 == null ? "" : model.rfid4;
        //                parameters[5].Value = model.rfid5 == null ? "" : model.rfid5;
        //                parameters[6].Value = model.status;
        //                parameters[7].Value = model.is_deleted;
        //                parameters[8].Value = model.remark;
        //                parameters[9].Value = model.create_date;
        //                parameters[10].Value = model.update_date;
        //                cmd.CommandText = sql;
        //                cmd.Parameters.AddRange(parameters);
        //                cmd.ExecuteNonQuery();
        //            }
        //            trans.Commit();
        //            return 0;
        //        }
        //        catch (SQLiteException ex)
        //        {
        //            log.Error("SaveBatch " + ex.Message);
        //            trans.Rollback();
        //            return -1;
        //        }
        //        finally
        //        {
        //            if (null != cmd) cmd.Dispose();
        //            if (null != conn) conn.Close();
        //        }
        //    }
        //}


        //public int SaveCollocationBatch(List<Collocation> list)
        //{
        //    if (list == null || list.Count < 1)
        //        return -1;
        //    DbProviderFactory factory = SQLiteFactory.Instance;
        //    using (DbConnection conn = factory.CreateConnection())
        //    {
        //        DbTransaction trans = null;
        //        DbCommand cmd = null;
        //        try
        //        {
        //            // 连接数据库  
        //            conn.ConnectionString = sqldb.ConnectionString;
        //            conn.Open();
        //            trans = conn.BeginTransaction();
        //            String sql = "";
        //            cmd = conn.CreateCommand();
        //            cmd.Connection = conn;

        //            sql = String.Format("DELETE FROM `recommend_show` where location='{0}' ", list[0].Loaction);
        //            cmd.CommandText = sql;
        //            cmd.ExecuteNonQuery();
        //            //保存新的数据
        //            foreach (Collocation model in list)
        //            {
        //                sql = "INSERT INTO `recommend_show` (`id`, `name`, `rfid`, `image`, `location`, `type`, `order`, `remark`)" +
        //                    " VALUES (@id, @name, @rfid, @image, @location, @type, @order, @remark);";
        //                SQLiteParameter[] parameters = {
        //                new SQLiteParameter("@id", DbType.String),
        //                new SQLiteParameter("@name", DbType.String),
        //                new SQLiteParameter("@rfid", DbType.String),
        //                new SQLiteParameter("@image", DbType.String), 
        //                new SQLiteParameter("@location", DbType.String),
        //                new SQLiteParameter("@type", DbType.String),
        //                new SQLiteParameter("@order", DbType.Int32,4),  
        //                new SQLiteParameter("@remark", DbType.String),};
        //                parameters[0].Value = Guid.NewGuid().ToString();
        //                parameters[1].Value = model.Name;
        //                parameters[2].Value = model.RFID;
        //                parameters[3].Value = model.image;
        //                parameters[4].Value = model.Loaction;
        //                parameters[5].Value = model.Type;
        //                parameters[6].Value = model.Order;
        //                parameters[7].Value = model.Remark;
        //                cmd.CommandText = sql;
        //                cmd.Parameters.AddRange(parameters);
        //                cmd.ExecuteNonQuery();
        //            }
        //            trans.Commit();
        //            return 0;
        //        }
        //        catch (SQLiteException ex)
        //        {
        //            log.Error("SaveBatch " + ex.Message);
        //            trans.Rollback();
        //            return -1;
        //        }
        //        finally
        //        {
        //            cmd.Dispose();
        //            conn.Close();
        //        }
        //    }
        //}


        //public List<Recommend> GetRecommendsLocal(IQuery query)
        //{
        //    List<Recommend> list = new List<Recommend>();
        //    if (null == query)
        //        return list;
        //    try
        //    {
        //        string sql = "";
        //        if (!String.IsNullOrEmpty(query.GetID()))
        //            sql = "SELECT id,rfid1,rfid2,rfid3,rfid4,rfid5,`status`,is_deleted,remark,create_date,update_date FROM recommend where id ='" + query.GetID() + "' ";
        //        else
        //            sql = "SELECT id,rfid1,rfid2,rfid3,rfid4,rfid5,`status`,is_deleted,remark,create_date,update_date FROM recommend ";

        //        DataTable dt = sqldb.ExecuteDataTable(CommandType.Text, sql.ToString());
        //        if (null != dt && dt.Rows.Count > 0)
        //        {
        //            Recommend model = null;
        //            foreach (DataRow row in dt.Rows)
        //            {
        //                model = new Recommend();
        //                model.id = Convert.ToString(row["id"]);
        //                model.rfid1 = Convert.ToString(row["rfid1"]);
        //                model.rfid2 = Convert.ToString(row["rfid2"]);
        //                model.rfid3 = Convert.ToString(row["rfid3"]);
        //                model.rfid4 = Convert.ToString(row["rfid4"]);
        //                model.rfid5 = Convert.ToString(row["rfid5"]);
        //                model.status = Convert.ToInt32(row["status"]);
        //                model.is_deleted = Convert.ToInt32(row["is_deleted"]);
        //                model.remark = Convert.ToString(row["remark"]);
        //                model.create_date = Convert.ToDateTime(row["create_date"]);
        //                model.update_date = Convert.ToDateTime(row["update_date"]);


        //                list.Add(model);
        //            }
        //        }

        //        return list;
        //    }
        //    catch (SQLiteException ex)
        //    {
        //        log.Error("GetRecommends " + ex.Message);
        //        list.Clear();
        //        return list;
        //    }
        //}

        ///// <summary>
        ///// 查询Collocation信息
        ///// </summary>
        ///// <param name="location"></param>
        ///// <returns></returns>
        //public List<Collocation> GetCollocationList(string location)
        //{
        //    List<Collocation> list = new List<Collocation>();
        //    try
        //    {
        //        string sql = "";
        //        if (!String.IsNullOrEmpty(location))
        //            sql = "SELECT `id`, `name`, `rfid`, `image`, `location`, `type`, `order`, `remark` FROM recommend_show where location ='" + location + "' ";
        //        else
        //            sql = "SELECT `id`, `name`, `rfid`, `image`, `location`, `type`, `order`, `remark` FROM recommend_show limit 0,10;";

        //        DataTable dt = sqldb.ExecuteDataTable(CommandType.Text, sql.ToString());
        //        if (null != dt && dt.Rows.Count > 0)
        //        {
        //            Collocation model = null;
        //            foreach (DataRow row in dt.Rows)
        //            {
        //                model = new Collocation();
        //                model.ID = Convert.ToString(row["id"]);
        //                model.Name = Convert.ToString(row["name"]);
        //                model.RFID = Convert.ToString(row["rfid"]);
        //                model.image = Convert.ToString(row["image"]);
        //                model.Loaction = Convert.ToString(row["location"]);
        //                model.Type = Convert.ToString(row["type"]);
        //                model.Order = Convert.ToInt32(row["order"]);
        //                model.Remark = Convert.ToString(row["remark"]);


        //                list.Add(model);
        //            }
        //        }

        //        return list;
        //    }
        //    catch (SQLiteException ex)
        //    {
        //        log.Error("GetCollocationList " + ex.Message);
        //        list.Clear();
        //        return list;
        //    }
        //}
        public int SaveBatch(List<Recommend> list)
        {
            throw new NotImplementedException();
        }

        public int AddRecommend(Recommend model)
        {
            throw new NotImplementedException();
        }

        public List<Recommend> GetRecommendsLocal(IQuery query)
        {
            throw new NotImplementedException();
        }

        public int DeleteById(string id)
        {
            throw new NotImplementedException();
        }

        public int SaveCollocationBatch(List<Collocation> list)
        {
            throw new NotImplementedException();
        }

        public List<Collocation> GetCollocationList(string location)
        {
            throw new NotImplementedException();
        }
    }
}
